<?php
error_reporting(0);
date_default_timezone_set('Europe/Amsterdam');

spl_autoload_register('__autoload'); // Make sure __autoload is called
	function __autoload($class)
{
	$paths = array
	(
		'model',
		'model/admin',
		'classes',
		'classes/mappers',
		'classes/forms',
		'classes/forms/validators'
	);
	
	foreach ($paths as $path)
	{
		$file = $path .'/' . $class .'.php';
		if(file_exists($file))
		{
			require_once $file;
			return;
		}
	}
	
}	
	// Live
	$sHOST = 'http://www.vasp.nl';	
	$sDB_HOST = 'localhost';	
	
	$sDB_USER = 'root';
	$sDB_NAME = 'nefub';
	$sDB_PASS = 'pass';
	

if($_SERVER['HTTP_HOST'] == 'www.vasp.nl')
{	
	// Live
	
}
else
{
	error_reporting(E_ALL ^ E_NOTICE);
	ini_set('display_errors', 'On');
	// Dev
	
	$sHOST = 'http://'.$_SERVER['HTTP_HOST'];

	
	if($_SERVER['HTTP_HOST'] == 'test.vasp.nl' )
	{
		$sDB_USER = $_SERVER['DBUSER'];
		$sDB_PASS = $_SERVER['DBPASSWD'];
		$sDB_NAME = 'nefub_test';
	}
	elseif(in_array($_SERVER['HTTP_HOST'], array('nefub.jmutsaerts.vm')))
	{
		$sDB_USER = $_SERVER['DBUSER'];
		$sDB_PASS = $_SERVER['DBPASSWD'];
		$sDB_NAME = 'nefub';
	}
	elseif(in_array($_SERVER['HTTP_HOST'], array('nefub-br.jmutsaerts.vm')))
	{
		$sDB_USER = $_SERVER['DBUSER'];
		$sDB_PASS = $_SERVER['DBPASSWD'];
		$sDB_NAME = 'nefub_br';
	}
	else
	{
		$sDB_USER = 'root';
		$sDB_PASS = 'pass';
		$sDB_NAME = 'raadselbak';
	}
}
	define('HOST', $sHOST);	
	define('DB_HOST', $sDB_HOST);	
	define('DB_USER', $sDB_USER);
	define('DB_NAME', $sDB_NAME);
	define('DB_PASS',  $sDB_PASS);
	
	define('LOGIN', md5('admin_is_logged_in'));
	
	define('NEFUB_ROOT', 'http://www.nefub.nl/competitie');
	define('NEFUB_API', 'http://www.nefub.nl/API');
	
	define('GAME_TYPE_COMPETITIE', 1);
	define('GAME_TYPE_BEKER', 2);
	define('GAME_TYPE_KLEINVELD', 3);
	
	define('GENDER_HEREN', 1);
	define('GENDER_DAMES', 2);
	define('GENDER_MIXED', 3);
	
	
	/*-------*/
	define('TYPE_GROOTVELD_NEFUB_NAME', 'Grootveld');
	define('TYPE_KLEINVELD_NEFUB_NAME', 'Kleinveld');
	
	define('GENRE_BEKER_NEFUB_NAME', 'beker');
	define('GENRE_COMPETITIE_NEFUB_NAME', 'competitie');	
	
	define('GENDER_HEREN_NEFUB_NAME', 'Heren');
	define('GENDER_DAMES_NEFUB_NAME', 'Dames');
	define('GENDER_MIXED_NEFUB_NAME', 'Mixed');
	
	
	
	

require_once 'var_dump_path.php';
require_once 'smarty/Smarty.class.php';
	$smarty = new Smarty();
	$smarty->setTemplateDir('templates');
	$smarty->setCompileDir('/smarty/templates_c');
	$smarty->setCacheDir('/smarty/cache');
	$smarty->setConfigDir('/smarty/config');

	
	
	

// ------------ lixlpixel recursive PHP functions -------------
// recursive_remove_directory( directory to delete, empty )
// expects path to directory and optional TRUE / FALSE to empty
// of course PHP has to have the rights to delete the directory
// you specify and all files and folders inside the directory
// ------------------------------------------------------------

// to use this function to totally remove a directory, write:
// recursive_remove_directory('path/to/directory/to/delete');

// to use this function to empty a directory, write:
// recursive_remove_directory('path/to/full_directory',TRUE);

function recursive_remove_directory($directory, $empty=FALSE)
{
	// if the path has a slash at the end we remove it here
	if(substr($directory,-1) == '/')
	{
		$directory = substr($directory,0,-1);
	}

	// if the path is not valid or is not a directory ...
	if(!file_exists($directory) || !is_dir($directory))
	{
		// ... we return false and exit the function
		return FALSE;

	// ... if the path is not readable
	}elseif(!is_readable($directory))
	{
		// ... we return false and exit the function
		return FALSE;

	// ... else if the path is readable
	}else{

		// we open the directory
		$handle = opendir($directory);

		// and scan through the items inside
		while (FALSE !== ($item = readdir($handle)))
		{
			// if the filepointer is not the current directory
			// or the parent directory
			if($item != '.' && $item != '..')
			{
				// we build the new path to delete
				$path = $directory.'/'.$item;

				// if the new path is a directory
				if(is_dir($path)) 
				{
					// we call this function with the new path
					recursive_remove_directory($path);

				// if the new path is a file
				}else{
					// we remove the file
					unlink($path);
				}
			}
		}
		// close the directory
		closedir($handle);

		// if the option to empty is not set to true
		if($empty == FALSE)
		{
			// try to delete the now empty directory
			if(!rmdir($directory))
			{
				// return false if not possible
				return FALSE;
			}
		}
		// return success
		return TRUE;
	}
}
	
	
	
function getConstantText($constant)
{
	$translations = array();	
	$translations[GENDER_HEREN]  = 'Heren';
	$translations[GENDER_DAMES]  = 'Dames';
	$translations[GENDER_MIXED]  = 'Mixed';
	
	$translations[GAME_TYPE_COMPETITIE]  = 'Competitie';
	$translations[GAME_TYPE_BEKER]  = 'Beker';
	$translations[GAME_TYPE_KLEINVELD]  = 'Kleinveld';
	
	if($translations[strtoupper($constant)])
	{
		return $translations[strtoupper($constant)];
	}	
	return $constant;
}


function convertDbFieldToTable($field)
{
	$field = preg_replace("/[0-9]/","", $field); 
	$field = str_replace('nefub_id', '', $field);
	$field = str_replace('id', '', $field);
	$parts = explode('_', $field);
	$table = '';
	foreach($parts as $part)
	{
		$table .= ucfirst(strtolower($part));
	}
	// exclusions
	$table = str_replace('Ref', '', $table);
	
	return $table;
}
/*
 * 
 * // Cool Class Diagram




RENAME TABLE
 competitions TO Competition,
 feedback TO FeedbackX,
 FeedbackX TO Feedback,
 games TO Game,
 game_actions TO GameAction, 
 game_persons TO GamePerson,
 locations TO Location,
 persons TO Person,
 players TO TeamPerson,
 poules TO Poule,
 poule_teams TO PouleTeam,
 retrieve_logs TO RetrieveLog,
 seasons TO Season,
 settings TO Setting,
 teams TO Team

De NeFUB API (in aanbouw) is te benaderen via www.nefub.nl/API/. Hier moet nog een handleiding en een uitbreiding op komen, maar vooralsnog kun je wedstrijden in JSON-formaat eruit exporteren. Dit doe je met de volgende calls:

- Schedule
De komende wedstrijden van deze persoon (inclusief rol: player, referee, match secretariat)
/schedule.php?persoon=<ID>

- Results
Voor de resultaten van afgelopen seizoen voor een persoon.
/results.php?persoon=<ID>

- Games
Samenvoeging van zowel schedule (komende wedstrijden) als results (gespeelde wedstrijden).
/games.php?persoon=<ID>


Je kunt de volgende parameters meegeven:

- Persoon
Gebruik het unieke ID van de persoon (895 ben ik bijvoorbeeld).
?persoon=895

- Team
Aanroep voor een team (750 is heren 3).
?team=750

- Vereniging
Alle wedstrijden voor een vereniging (8 is UFC Utrecht).
?vereniging=8


Wat betreft de hallen. Ik kan hier binnenkort een call voor maken die alle hallen weergeeft. Vooralsnog heb ik op de wedstrijddetailpagina de lokatie linkable gemaakt. Dan kun je dit parsen voor het betreffende ID. Zie bijgaande CSV voor alle lokaties van dit moment.


TRUNCATE TABLE Competition;
TRUNCATE TABLE Game;
TRUNCATE TABLE GameAction;
TRUNCATE TABLE GamePerson;
TRUNCATE TABLE Location;
TRUNCATE TABLE Person;
TRUNCATE TABLE TeamPerson;
TRUNCATE TABLE Poule;
TRUNCATE TABLE Team;
TRUNCATE TABLE PouleTeam;
*/
